<template>
	<view class="cate_search">
		<view class="cate_search__box">
			<uni-search-bar :focus="true" @input="searchInputHandler" cancelButton="none" radius="100"></uni-search-bar>
		</view>
		<view class="search_content__container">
			<view v-if="searchResults.length !== 0 && visibleFlag" class="search_list__container">
				<view class="search_item" v-for="(item, index) in searchResults" :key="index" @click="gotoDetail(item)">
					<view class="goods_name">{{item.goods_name}}</view>
					<uni-icons type="forward" size="16"></uni-icons>
				</view>
			</view>
			<view v-else class="search_history__container">
				<view v-show="searchTitleVisible" class="search_history__title">
					<text class="history_title__text">搜索历史</text>
					<uni-icons class="goods_icons" type="trash" size="18" @click="clearHistory"></uni-icons>
				</view>
				<view class="history_list">
					<uni-tag class="history_tag" :text='item' :circle="true" v-for="(item, index) in histories" :key="index" @click="gotoGoodsListt(item)"></uni-tag>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { connnectApi } from '../../static/api'
	import { showMsg } from '../../static/common'
	export default {
		data() {
			return {
				searchText: '',
				timer: null,
				searchResults: [],
				historyList: [],
				visibleFlag: false,
				searchTitleVisible: false,
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('searchHistory' || []));
		},
		computed: {
			histories() {
				return [...this.historyList].reverse();
			},
		},
		methods: {
			searchInputHandler(text) {
				clearTimeout(this.timer);
				this.timer = setTimeout(() => {
					this.visibleFlag = text !== '' ? true : false;
					this.searchText = text;
					this.getSearchList(text);
					
				}, 500)
			},
			async getSearchList(text) {
				const queryParams = {
					query: text,
				};
				await connnectApi.getSearchList(queryParams)
				.then(({data}) => {
					if(data.meta.status !== 200) return;
					this.searchResults = data.message;
					if(this.searchResults.length === 0)	return showMsg.showErrMsg('查无此结果！')
					this.saveSearchHistory();
				})
				.catch((err) => {
					console.log(err);
				});
			},
			gotoDetail(item) {
				uni.navigateTo({
					url: '../goods_details/goods_details?goods_id=' + item.goods_id,
				})
			},
			gotoGoodsListt(searchText) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + searchText,
				})
			},
			
			saveSearchHistory() {
				// this.historyList.push(this.searchText);
				const set = new Set(this.historyList);
				set.delete(this.searchText);
				set.add(this.searchText);
				this.historyList = Array.from(set);
				uni.setStorageSync('searchHistory', JSON.stringify(this.historyList));
				this.searchTitleVisible = true;
			},
			clearHistory() {
				this.historyList = [];
				uni.setStorageSync('searchHistory', []);
				this.searchTitleVisible = false;
			},
			clear(res) {},
		},
	}
</script>

<style lang="scss">
.cate_search {
	.cate_search__box {
		position: sticky;
		top: 0;
		
		z-index: 9999999;
	}
	.search_content__container {
		padding: 10rpx;
		.search_list__container {
			padding: 5px 10px;
			.search_item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 13rpx 0;
				font-size: 10rpx;
				border-bottom: 1px solid #efefef;
				.goods_name {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					margin-right: 5rpx;
				}
			}
		}
		.search_history__container {
			.search_history__title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				.history_title__text {
					color: #ccc;
				}
				.goods_icons {
					margin-right: 10rpx;
				}
			}
			.history_list {
				margin-top: 10px;
				.history_tag {
					margin-right: 5rpx;
				}
			}
		}
	}
}

</style>
